<template>
  <div class="search">
    <Card>
      <Form
        ref="searchForm"
        :model="searchForm"
        inline
        :label-width="70"
        class="search-form"
        @keydown.enter.native="handleSearch"
      >
        <Form-item label="门店名称" prop="storeName">
          <Input
            type="text"
            v-model="searchForm.storeName"
            placeholder="请输入门店名称"
            clearable
            style="width: 200px"
          />
        </Form-item>
        <Button @click="handleSearch" class="search-btn" type="primary" icon="ios-search">搜索</Button>
      </Form>

      <Row class="operation padding-row">
        <Button @click="add" type="primary">添加门店</Button>
      </Row>
      <Table :loading="loading" border :columns="columns" :data="data" ref="table" class="mt_10"></Table>
      <Row type="flex" justify="end" class="mt_10">
        <Page
          :current="searchForm.pageNumber"
          :total="total"
          :page-size="searchForm.pageSize"
          @on-change="changePage"
          @on-page-size-change="changePageSize"
          :page-size-opts="[10, 20, 50]"
          size="small"
          show-total
          show-elevator
          show-sizer
        ></Page>
      </Row>
    </Card>

    <!-- 添加用户模态框 -->
    <Modal v-model="addFlag" title="添加门店">
      <Form ref="addForm" :model="form" :rules="addRule" :label-width="100">
        <FormItem label="门店名称" prop="storeName">
          <Input v-model="form.storeName" style="width: 200px" />
        </FormItem>
        <FormItem label="门店地址" prop="address">
          <Input
            v-model="form.address"
            @on-focus="$refs.liliMap.showMap = true"
            clearable
            style="width: 200px"
          />
        </FormItem>
        <FormItem label="门店相片：">
          <upload-pic-thumb v-model="form.storePhotos" :multiple="false"></upload-pic-thumb>
        </FormItem>
        <FormItem label="店长" prop="shopowner">
          <Select
            v-model="form.shopowner"
            filterable
            :remote-method="getAllMemberList"
            :loading="loading"
            :filter-by-label="true"
            :popper-append-to-body="false"
            popper-class="spec-values-popper"
            style="width: 100%; text-align: left; margin-right: 10px"
          >
            <Option
              v-for="item in shopownerList"
              :value="item.id"
              :label="item.nickName"
              :key="item.id"
            ></Option>
          </Select>
        </FormItem>
      </Form>
      <div slot="footer">
        <Button @click="addFlag = false">取消</Button>
        <Button type="primary" @click="addSubmit">确定</Button>
      </div>
      <liliMap ref="liliMap" @getAddress="getAddress"></liliMap>
    </Modal>

    <!-- 修改模态框 -->
    <Modal v-model="editFlag" title="修改门店" @on-ok="handleSubmitModal" width="500">
      <Form ref="editForm" :model="form" :rules="editRule" :label-width="80">
        <Input v-model="form.id" v-show="false" />
        <FormItem label="门店名称" prop="storeName">
          <Input v-model="form.storeName" style="width: 200px" />
        </FormItem>
        <FormItem label="门店地址" prop="address">
          <Input
            v-model="form.address"
            @on-focus="$refs.liliMap.showMap = true"
            clearable
            style="width: 200px"
          />
        </FormItem>
        <FormItem label="门店相片">
          <upload-pic-thumb v-model="form.storePhotos" :multiple="false"></upload-pic-thumb>
        </FormItem>
        <FormItem label="店长" prop="shopowner">
          <Select
            v-model="form.shopowner"
            filterable
            :filter-by-label="true"
            :popper-append-to-body="false"
            popper-class="spec-values-popper"
            style="width: 100%; text-align: left; margin-right: 10px"
          >
            <Option
              v-for="item in shopownerList"
              :value="item.id"
              :label="item.nickName"
              :key="item.id"
            ></Option>
          </Select>
        </FormItem>
      </Form>
    </Modal>
  </div>
</template>

<script>
import {
  getShopInfoListData,
  shopInfoEdit,
  shopInfoDel,
  shopInfoAdd
} from "@/api/shops";
import { getAllMemberList } from "@/api/member";
import liliMap from "@/components/map/index";
import uploadPicThumb from "@/components/lili/upload-pic-thumb";
import vueQr from "vue-qr";
export default {
  components: {
    "vue-qr": vueQr,
    liliMap,
    uploadPicThumb
  },
  name: "shopInfo",
  data() {
    return {
      shopownerList: [],
      form: {
        id: "",
        storeName: "",
        storePhotos: "",
        shopowner: "",
        address: "",
        longitude: "",
        latitude: ""
      }, // 表单数据
      addRule: {
        // 验证规则
        storeName: [{ required: true, message: "请输入门店名称" }],
        shopowner: [{ required: true, message: "请选择店长" }],
        address: [{ required: true, message: "请选择门店地址" }]
      },
      editRule: {
        // 验证规则
        storeName: [{ required: true, message: "请输入门店名称" }],
        shopowner: [{ required: true, message: "请选择店长" }],
        address: [{ required: true, message: "请选择门店地址" }]
      }, //修改验证
      editFlag: false, //编辑查看框
      addFlag: false, //新增查看框
      id: "", //要操作的id
      loading: true, // 表单加载状态
      modalVisible: false, // 添加或编辑显示
      searchForm: {
        // 搜索框初始化对象
        pageNumber: 1, // 当前页数
        pageSize: 10, // 页面大小
        sort: "create_time", // 默认排序字段
        order: "desc" // 默认排序方式
      },
      submitLoading: false, // 添加或编辑提交状态
      columns: [
        {
          title: "门店名称",
          key: "storeName",
          minWidth: 100,
          tooltip: true
        },
        {
          title: "门店地址",
          key: "address",
          minWidth: 100,
          tooltip: true
        },
        {
          title: "店长名称",
          key: "nickName",
          minWidth: 100,
          tooltip: true
        },
        {
          title: "店长电话",
          key: "mobile",
          minWidth: 100,
          tooltip: true
        },
        {
          title: "操作",
          key: "action",
          align: "center",
          fixed: "right",
          width: 150,
          render: (h, params) => {
            return h("div", [
              h(
                "Button",
                {
                  props: {
                    size: "small"
                  },
                  style: {
                    marginRight: "5px"
                  },
                  on: {
                    click: () => {
                      this.edit(params.row);
                    }
                  }
                },
                "修改"
              ),
              h(
                "Button",
                {
                  props: {
                    type: "info",
                    size: "small"
                  },
                  on: {
                    click: () => {
                      this.del(params.row);
                    }
                  }
                },
                "删除"
              )
            ]);
          }
        }
      ],
      data: [], // 表单数据
      total: 0 // 表单数据总数
    };
  },
  methods: {
    // 初始化数据
    init() {
      this.getDataList();
      this.getAllMemberList();
    },
    // 分页 改变页码
    changePage(v) {
      this.searchForm.pageNumber = v;
      this.getDataList();
    },
    // 分页 改变页数
    changePageSize(v) {
      this.searchForm.pageSize = v;
      this.getDataList();
    },
    // 搜索
    handleSearch() {
      this.searchForm.pageNumber = 1;
      this.searchForm.pageSize = 10;
      this.getDataList();
    },
    // 获取会员数据
    getAllMemberList(e) {
      getAllMemberList({ search: e }).then(res => {
        if (res.success) {
          this.shopownerList = res.result;
          console.log(this.shopownerList);
        }
      });
    },
    // 获取数据
    getDataList() {
      this.loading = true;
      getShopInfoListData(this.searchForm).then(res => {
        this.loading = false;
        if (res.success) {
          this.data = res.result.records;
          this.total = res.result.total;
        }
      });
    },
    //获取地址
    getAddress(item) {
      // this.$set(this.form, "consigneeAddressPath", item.addr);
      // this.$set(this.form, "consigneeAddressIdPath", item.addrId);
      this.form.address = item.address;
      this.form.latitude = item.position.lat;
      this.form.longitude = item.position.lng;
    },
    // 新增
    add(v) {
      this.$refs.addForm.resetFields();
      this.form.id = "";
      this.form.storeName = "";
      this.form.shopowner = "";
      this.form.address = "";
      this.form.latitude = "";
      this.form.longitude = "";
      this.form.storePhotos = "";

      this.addFlag = true;
    },
    // 编辑
    edit(v) {
      this.form.id = v.id;
      this.form.storeName = v.storeName;
      this.form.shopowner = v.shopowner;
      this.form.address = v.address;
      this.form.latitude = v.latitude;
      this.form.longitude = v.longitude;
      this.form.storePhotos = v.storePhotos || "";
      this.editFlag = true;
    },
    //提交添加数据
    addSubmit() {
      this.$refs.addForm.validate(valid => {
        if (valid) {
          shopInfoAdd(this.form).then(res => {
            if (res.result) {
              this.$refs.addForm.resetFields();
              this.getDataList();
              this.$Message.success("添加成功！");
              this.addFlag = false;
            }
          });
        }
      });
    },
    // 提交修改数据
    handleSubmitModal() {
      shopInfoEdit(this.form).then(res => {
        if (res.result) {
          this.$Message.success("修改成功！");
          this.init();
        }
      });
    },
    // 删除
    del(v) {
      let ids = [v.id];
      this.$Modal.confirm({
        title: "提示",
        content: "<p>是否删除？</p>",
        onOk: () => {
          shopInfoDel(ids).then(res => {
            this.$Modal.remove();
            if (res.success) {
              this.$Message.success("删除成功");
              this.getDataList();
            }
          });
        }
      });
    }
  },
  mounted() {
    this.init();
  }
};
</script>
